<?php

/**
 * import_machine actions.
 *
 * @package    maslogger
 * @subpackage import_machine
 * @author     Michaël VEROUX
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class import_machineActions extends sfActions
{
    private $filename = '/tmp/import_machine.csv';
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
    $this->form = new ImportCsvForm();
    if ($request->isMethod('post')) {
        $this->processForm($request, $this->form);
    }
  }
  public function executeParse(sfWebRequest $request){
      $csv = new ImportCsvColl(file_get_contents(sfConfig::get('sf_upload_dir').$this->filename));
      if(!$request->isMethod('post')){
        $this->form = $csv->getRenderRequiredKeys();
      }else{
        $champs = $request->getParameter('champs');
        foreach($champs AS $key => $champ){
            $csv->bindWidth($key, $champ);
        }
        $this->nb_machines = $csv->saveFromCsv();
      }
  }
  protected function processForm(sfWebRequest $request, sfForm $form)
    {
      $form->bind(
        $request->getParameter('import'),
        $request->getFiles('import')
      );

      if ($form->isValid())
      {
        $file = $form->getValue('csv_file');
        $file->save(sfConfig::get('sf_upload_dir').$this->filename);

        $this->redirect('import_machine/parse');
      }
    }
}
